package com.trolls.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ViewRaceChoice extends Content {
	
	/**
	 * This is the entry point method.
	 */
	private Race raceSelected;
	private Troll trollEdit = null;
	private CompositePortraitUi comp_portait;
	private CompositeGalleryImg comp_gallery;
	private TabPanel tPanel;
	private VerticalPanel vPanel;
	/**
	 * This is the entry point method.
	 */
	public ViewRaceChoice(ArrayList<Item> listRace,Troll trollEdite) {
		super("Choisir la race de "+trollEdite.getName());
		this.trollEdit = trollEdite;
		raceSelected = trollEdite.getRace();
		config(listRace);
		this.initWidget(vPanel);

	}
	public ViewRaceChoice(ArrayList<Item> listRace) {
		super("Choisir la race");
		trollEdit = new Troll(0, null, null, null, null, 10, 10, 10, 10, 10, 10, 10);
		
		raceSelected = (Race) listRace.get(0);
		config(listRace);
		
		this.initWidget(vPanel);

	}
	private void config(ArrayList<Item> listRace) {
		// Création du tableau
				vPanel = new VerticalPanel();
				VerticalPanel vPanelInfo = new VerticalPanel();
				vPanel.add(enteteContent);
				
				tPanel = new TabPanel();
				tPanel.getElement().setId("tabPanel");
				HorizontalPanel hPanel = new HorizontalPanel();
				HorizontalPanel hPanelGallery = new HorizontalPanel();


				// Ajout des onglets
				HTML description = new HTML();
				HTML caracteristique = new HTML();
				tPanel.add(description, "Histoire");
				tPanel.add(caracteristique, "Caracteristiques");
				tPanel.selectTab(0);
				comp_portait = new CompositePortraitUi(raceSelected.getImagePath(),raceSelected.getName());
				comp_portait.getTbName().setEnabled(false);
				comp_gallery = new CompositeGalleryImg(listRace);
				hPanelGallery.add(comp_gallery);
				hPanelGallery.setVerticalAlignment(HasAlignment.ALIGN_BOTTOM);
				
				hPanel.add(comp_portait);
				vPanelInfo.add(tPanel);
				hPanel.setVerticalAlignment(HasAlignment.ALIGN_TOP);
				vPanel.add(hPanel);
				HorizontalPanel hPanelSectionner = new  HorizontalPanel();
				PushButton buttonSelectionner = new PushButton("Selectionner");
				hPanelSectionner.setStyleName("hPanelSectionner");
				buttonSelectionner.addClickHandler(new ClickHandler() {

					public void onClick(ClickEvent event) {
						trollEdit.setRace(raceSelected);
						LaTerreDesTrolls.get().editTrollWithRace(trollEdit);
					}
				});
				
				hPanelSectionner.add(buttonSelectionner);
				vPanelInfo.add(hPanelSectionner);
				hPanel.add(vPanelInfo);

				vPanel.add(hPanelGallery);
				showRace(raceSelected);

	}


	public void showRace(Race race){
		raceSelected = race;
		Image img = new Image(race.getImagePath());
		img.addStyleName("gallery-img");
		img.setTitle(race.getName());
		img.setAltText(race.getName());
		
		comp_portait.setImg(img);
		tPanel.getWidget(0).getElement().setInnerHTML(race.getDescription());
		tPanel.getWidget(1).getElement().setInnerHTML("Compétence spéciale : "+race.getCompetenceSpeciale()+"<br/>Compétence spéciale Secondaire: "+race.getCompetenceSpecialeSecondaire());

	}
}
